package com.aiyige.page.my.wallet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.page.my.wallet.dialog.VerifyWalletPwdDialog;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.PreferenceUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.TextWatcherAdapter;
import com.aiyige.utils.ToastX;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 钱包-提现
 */
@Route(path = ARouterConfig.WalletWithdrawalPage, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class WalletWithdrawalPage extends BaseActivity implements VerifyWalletPwdDialog.Listener {

    @BindView(R.id.et_alipay_account)
    EditText etAlipayAccount;
    @BindView(R.id.et_alipay_username)
    EditText etAlipayUsername;
    @BindView(R.id.et_withdrawal_amount)
    EditText etWithdrawalAmount;
    @BindView(R.id.tv_balance)
    TextView tvBalance;

    @BindView(R.id.btn_submit)
    Button btnSubmit;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_wallet_withdrawal);
        ButterKnife.bind(this);
        initTitleLayout(getString(R.string.withdrawal));
        setAutoRegisterEventBus(true);
        etAlipayAccount.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                checkInput();
            }
        });

        etAlipayUsername.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                checkInput();
            }
        });

        etWithdrawalAmount.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                checkInput();
            }
        });
        double balance = AccountUtil.getCurrentUser().getStatisticsBackup().getPurseBalance();
        tvBalance.setText(getString(R.string.the_current_wallet_balance, StringUtils.priceFormat(balance)));

        String alipayAccount = PreferenceUtil.getString("alipayAccount"+AccountUtil.getCurrentUser().getId(), getAlipayAccount());
        if (!isNull(alipayAccount)) {
            etAlipayAccount.setText(alipayAccount);
        }
        String alipayUsername = PreferenceUtil.getString("alipayUsername"+AccountUtil.getCurrentUser().getId(), getAlipayUsername());
        if (!isNull(alipayAccount)) {
            etAlipayUsername.setText(alipayUsername);
        }
    }

    private void checkInput() {
        if (!TextUtils.isEmpty(getAlipayAccount()) && !TextUtils.isEmpty(getAlipayUsername())
                && !TextUtils.isEmpty(getWithdrawalAmount())) {
            btnSubmit.setEnabled(true);
        } else {
            btnSubmit.setEnabled(false);
        }

    }

    private String getAlipayAccount() {
        return etAlipayAccount.getText().toString().trim();
    }

    private String getAlipayUsername() {
        return etAlipayUsername.getText().toString().trim();
    }

    private String getWithdrawalAmount() {
        return etWithdrawalAmount.getText().toString().trim();
    }
//
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onSetPwdSuccess(EventSetWalletPwdSuccess eventSetWalletPwdSuccess) {
//        if (eventSetWalletPwdSuccess != null) {
//            finish();
//        }
//    }


    @OnClick({R.id.btn_submit, R.id.allWithdrawBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                String price = etWithdrawalAmount.getText().toString();
                try {
                    if (!TextUtils.isEmpty(price) && price.contains(".")) {
                        String decimals[] =     price.split("\\.");
                        if (!TextUtils.isEmpty(decimals[1]) && decimals[1].length() > 2) {
                            ToastX.show("提现金额小数点最多2位");
                            return;
                        }
                    }
                } catch (Exception e) {

                }
                VerifyWalletPwdDialog dialog = VerifyWalletPwdDialog.newInstance();
                dialog.setLisenter(this);
                dialog.show(getSupportFragmentManager(), "");
                break;
            case R.id.allWithdrawBtn:
                double balance = AccountUtil.getCurrentUser().getStatisticsBackup().getPurseBalance();
                etWithdrawalAmount.setText(StringUtils.priceFormat(balance));
                break;
        }
    }

    @Override
    public void onWithdrawalFinish() {
        PreferenceUtil.putString("alipayAccount"+AccountUtil.getCurrentUser().getId(), getAlipayAccount());
        PreferenceUtil.putString("alipayUsername"+AccountUtil.getCurrentUser().getId(), getAlipayUsername());
        ARouter.getInstance()
                .build(ARouterConfig.WalletWithdrawalResultPage)
                .navigation();
        finish();
    }

    @Override
    public String getAccount() {
        return etAlipayAccount.getText().toString().trim();
    }

    @Override
    public String getName() {
        return etAlipayUsername.getText().toString().trim();
    }

    @Override
    public double getAmount() {
        String amount = etWithdrawalAmount.getText().toString().trim();
        if (isNull(amount)) {
            return 0;
        }
        return Double.parseDouble(amount);
    }
}
